package com.luhuiguo.cms.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.luhuiguo.cms.client.AppConstants;
import com.luhuiguo.cms.client.AppMessages;
import com.luhuiguo.cms.client.AppValidationMessages;
import com.luhuiguo.cms.client.presenter.EditChannelPresenter;
import com.luhuiguo.gwt.validation.client.DefaultValidationProcessor;
import com.luhuiguo.gwt.validation.client.ValidationProcessor;
import com.luhuiguo.gwt.validation.client.actions.FocusAction;
import com.luhuiguo.gwt.validation.client.actions.StyleAction;
import com.luhuiguo.gwt.validation.client.description.PopupDescription;
import com.luhuiguo.gwt.validation.client.description.PopupDescription.Location;
import com.luhuiguo.gwt.validation.client.validators.numeric.IntegerValidator;
import com.luhuiguo.gwt.validation.client.validators.standard.RegularExpressionValidator;
import com.luhuiguo.gwt.validation.client.validators.strings.StringLengthValidator;

public class EditChannelView extends Composite implements EditChannelPresenter.Display {

    private static EditChannelViewUiBinder uiBinder = GWT.create(EditChannelViewUiBinder.class);

    interface EditChannelViewUiBinder extends UiBinder<Widget, EditChannelView> {
    }

    @SuppressWarnings("unused")
    private AppConstants constants;

    @SuppressWarnings("unused")
    private AppMessages messages;

    private ValidationProcessor validator;

    private PopupDescription description;

    @UiField
    TextBox nameField;

    @UiField
    TextBox titleField;

    @UiField
    TextBox rankField;

    @UiField
    Button submitButton;

    @UiField
    Button cancelButton;

    @Inject
    public EditChannelView(AppConstants constants, AppMessages messages, AppValidationMessages validationMessages) {

        this.constants = constants;
        this.messages = messages;

        initWidget(uiBinder.createAndBindUi(this));
        validator = new DefaultValidationProcessor(validationMessages);
        description = new PopupDescription(validationMessages, Location.RIGHT);

        description.addDescription("nameHint", nameField);
        description.addDescription("titleHint", titleField);
        description.addDescription("rankHint", rankField);

        FocusAction fAction = new FocusAction();
        StyleAction sAction = new StyleAction(constants.validationFailedBorder());
        validator.addValidators(
                "name",
                new RegularExpressionValidator(nameField, constants.nameRegex(), constants.nameMsgKey())
                        .addActionForFailure(fAction).addActionForFailure(sAction))
                .addValidators(
                        "title",
                        new StringLengthValidator(titleField, 1, 100).addActionForFailure(fAction).addActionForFailure(
                                sAction)).addValidators("rank",
                        new IntegerValidator(rankField).addActionForFailure(fAction).addActionForFailure(sAction));

        GWT.log("EditChannelView created");
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public HasValue<String> getNameField() {
        return nameField;
    }

    @Override
    public HasValue<String> getTitleField() {
        return titleField;
    }

    @Override
    public HasClickHandlers getSubmitButton() {
        return submitButton;
    }

    @Override
    public HasClickHandlers getCancelButton() {
        return cancelButton;
    }

    @Override
    public boolean validate() {
        return validator.validate();
    }

    @Override
    public ValidationProcessor getValidator() {
        return validator;
    }

    @Override
    public void setLoading(boolean loading) {
        if (loading) {
            // loading.show();
            submitButton.setEnabled(false);
            cancelButton.setEnabled(false);
        } else {
            // loading.hide();
            submitButton.setEnabled(true);
            cancelButton.setEnabled(true);
        }

    }

    @Override
    public void setNameEnabled(boolean enabled) {
        nameField.setEnabled(enabled);

    }

    @Override
    public HasValue<String> getRankField() {
        return rankField;
    }

}
